#include <stdlib.h>
#include <stdio.h>
#include "cardMenu.h"
#include "GL/glut.h"
#include "cardCharSelect.h"
#include "string3d.h"

CardMenu::CardMenu()
{
	/* Meshes */
	brick1.load("obj/brick_00.ply", 0);
	brick2.load("obj/brick_01.ply", 1);
	ball  .load("obj/ball.ply"    , 0);
	stage .load("obj/stage_01.ply", 0);

	//Translations
	brick1.translateSet(-3.0f, -1.5f, 0.0f);
	brick2.translateSet( 3.0f, -1.5f, 0.0f);
	ball  .translateSet( 0.0f, brick1.y() +brick1.pbound.y +ball.pbound.y, 0.0f);
	stage .translateSet( 0.0f, brick1.y() +brick1.nbound.y -1.0f, 0.0f);
	stage.scaleSet(1.5f, 1.5f, 1.5f);

	//Logo
	logoStyle.setColorFront(0.133f, 0.133f, 0.133f);
	logoStyle.setColorBack (0.188f, 0.292f, 0.530f);
	logoStyle.setAlign(1);
	matLogo.translateSet(0.0f, 2.5f, 4.0f);
	matLogo.scaleSet(1.3f, 1.3f, 1.3f);
	logo = (char*) "dargite3d";

	//Text choices
	matText[0].translateSet(0.0f, 1.5f -(1.0f *0.9f), 4.0f);
	matText[1].translateSet(0.0f, 1.5f -(2.0f *0.9f), 4.0f);
//	matText[2].translateSet(0.0f, 1.0f -(2.0f *0.9f), 4.0f);
	text[0] = (char*) "start";
	//text[1] = (char*) "survival";
	text[1] = (char*) "exit";

	/* Initial Rotation (rotates the background meshes) */
	rotation = 20.0f;

	//Init selection
	selection = 0;

	//Selection font
	font.setColorFront(0.9f, 0.327f, 0.689f);
	font.setColorBack (0.26f, 0.121f, 0.256f);
	font.setAlign(1);
}

void CardMenu::input(unsigned char key, bool pressed)
{
	//Accept input only if animation is not running
	if (!anim.isRunning())
	//Act according to key
	switch (key)
	{
		//Next selection (if no more selections, select first one)
		case 's':
			if (pressed && ++selection > 1)
				selection = 0;
			break;

		//Previous selection (if no previous selection, select last one)
		case 'w':
			if (pressed && --selection < 0)
				selection = 1;
			break;

		//Space key
		case '\040':
			//Key released
			if (!pressed)
				//Start animation
				anim.startFromStart();
	}
}

void CardMenu::update()
{
	//Animation changed
	if (anim.update())
		//Animation reached end
		if (anim.isAtEnd())
			//Act accoring to current selection
			switch (selection)
			{
				//Start normal mode
				case 0:
					//Delete menu
					delete card;
	
					//Start character select screen
					card = new CardCharSelect(0);
					return;
					
				//Start survival
				/*case 1:
					//Delete menu
					delete card;
	
					//Start character select screen
					card = new CardCharSelect(1);
					return;*/

				//Exit
				case 1:	exit(0);
			}

	//Keep rotating
	rotation += 0.2f;
	if (rotation > 360.0f)
		rotation -= 360.0f;
}

void CardMenu::draw()
{
	//Card's animation
	Card::draw();

	//Camera translation data
	glTranslatef(0.0f, -1.0f, -10.0f);

	//Draw text choices
	int i;
	for (i = 0; i < 2; i++)
	{
		//Selected text
		if (selection == i)
		{
			//Create quake effect

			//Seperate X and Y
			float tempX = ((rand() +1) %100) /1000.0f;
			float tempY = ((rand() +1) %100) /1000.0f;

			//Add effect to matrix
			matText[i].translateAdd(tempX, tempY, 0.0f);

			//Draw choice
			matText[i].push_apply();
			font.draw(text[i]);
			glPopMatrix();

			//Remove effect from matrix
			matText[i].translateAdd(-tempX, -tempY, 0.0f);
		}
		//Other text choices
		else 
		{
			//Simply draw them
			matText[i].push_apply();
			font.draw(text[i]);
			glPopMatrix();
		}
	}

	//Draw logo
	matLogo.push_apply();
	logoStyle.draw(logo);
	glPopMatrix();

	//Rotate camera
	glRotatef(rotation, 0.0f, 1.0f, 0.0f);
	glRotatef(20.0f, 1.0f, 0.0f, 0.0f);

	//Draw bricks, ball and stage
	stage.draw();
	brick1.draw();
	brick2.draw();
	ball.draw();
}
